package com.platform.qujia.service.impl;

import com.alibaba.fastjson.JSON;
import com.platform.qujia.dao.StaffMapper;
import com.platform.qujia.dao.StationMapper;
import com.platform.qujia.dao.SysDictDataMapper;
import com.platform.qujia.pojo.StaOilGun;
import com.platform.qujia.pojo.Station;
import com.platform.qujia.pojo.vo.OilGunVO;
import com.platform.qujia.pojo.vo.StaffVO;
import com.platform.qujia.service.StaffService;
import com.platform.qujia.utils.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @date 2023/8/8
 */
@Slf4j
@Service
public class StaffServiceImpl implements StaffService {

    public static final String DIC_TYPE_STAFF_SETTING = "staff_setting";
    public static final String DIC_LABEL_DEFAULT_ROLE_ID = "default_role_id";
    public static final String DIC_LABEL_DEFAULT_DEPT_ID = "default_dept_id";

    public static final String DIC_LABEL_STAFF_ROLE_ID = "staff_role_id";

    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private StationMapper stationMapper;


    @Override
    public OilGunVO queryStaffOilList(Long stationId, Long userId) {
        log.info("queryStaffOilList 入参 ：油站；{},用户：{}", stationId, userId);
        //油站员工是否必选
        Station station = staffMapper.getStation(stationId);
        if (station == null) {
            throw new CustomException("油站不存在");
        }
        boolean needStaff = StringUtils.equals("1", station.getNeedStaff());
        OilGunVO oilGunVO = new OilGunVO();
        oilGunVO.setNeedStaff(needStaff);
        oilGunVO.setIsTube(station.getIsTube());
        oilGunVO.setIsInput(station.getIsInput());
        oilGunVO.setHasUserId(false);
        if (userId != null && userId != 0) {
            oilGunVO.setStaff(getStaffInfo(stationId, userId));
            oilGunVO.setHasUserId(true);
            return oilGunVO;
        }
        if (needStaff) {
            //油站设置员工必选
            oilGunVO.setStaffList(queryStaffList(stationId));
            return oilGunVO;
        }
        oilGunVO.setGunList(queryStationGunList(stationId));
        log.info("queryStaffOilList 出参 ：{}", JSON.toJSONString(oilGunVO));
        return oilGunVO;
    }

    private StaffVO getStaffInfo(Long stationId, Long userId) {
        //String roleId = dictDataMapper.selectDictValue(DIC_TYPE_STAFF_SETTING, DIC_LABEL_STAFF_ROLE_ID);
        //StaffVO staff = staffMapper.getStaffInfo(userId,Integer.parseInt(roleId));
        StaffVO staff = staffMapper.getStaffInfo(userId, null);
        if (staff == null) {
            throw new CustomException("加油员不存在");
        }
        String gunNames = staff.getGun();
        if (StringUtils.isBlank(gunNames)) {
            staff.setGunList(Collections.emptyList());
            return staff;
        }
        String[] split = gunNames.split(",");
        List<StaOilGun> staOilGuns = queryStationGunList(stationId);
        Iterator<StaOilGun> iterator = staOilGuns.iterator();
        while (iterator.hasNext()) {
            StaOilGun oilGun = iterator.next();
            if (!StringUtils.equalsAny(oilGun.getGunName(), split)) {
                iterator.remove();
            }
        }
        staff.setGunList(staOilGuns);
        return staff;
    }

    private List<StaOilGun> queryStationGunList(Long stationId) {
        List<StaOilGun> staOilGun = staffMapper.queryStationOilGunList(stationId);
        List<StaOilGun> oilGuns = new ArrayList<>();
        for (StaOilGun oilGun : staOilGun) {
            String names = oilGun.getGunName();
            if (StringUtils.isNotBlank(names)) {
                String[] split = names.split(",");
                for (String s : split) {
                    if (StringUtils.isNumeric(s)) {
                        StaOilGun gun = new StaOilGun();
                        gun.setGunNameNum(Integer.parseInt(s));
                        gun.setGunName(s);
                        gun.setOilStationPrice(oilGun.getOilStationPrice());
                        gun.setOilId(oilGun.getOilId());
                        gun.setStationId(oilGun.getStationId());
                        gun.setOilOfficialPrice(oilGun.getOilOfficialPrice());
                        gun.setOil(oilGun.getOil());
                        oilGuns.add(gun);
                    }
                }
            }
        }
        oilGuns.sort(Comparator.comparingInt(StaOilGun::getGunNameNum));
        return oilGuns;
    }

    @Override
    public List<StaffVO> queryStaffList(Long stationId) {
        Map<String, Object> params = new HashMap<>();
        params.put("stationId", stationId);
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        String formattedStartTime = format.format(date);
        params.put("createEndTime", formattedStartTime);
        String classesDayOrNight =   stationMapper.queryClassDayOrNight(params);
        List<String> userIds = new ArrayList<>();

        if(classesDayOrNight.equals("-")){

        }else if (classesDayOrNight.equals("day")){
            Station stationTime  =  stationMapper.selectStationById(stationId.intValue());
            if (stationTime.getDayShiftUserIds() != null && stationTime.getDayShiftUserIds() != "") {
                String[] uniqueActivityIds = stationTime.getDayShiftUserIds().split(",");
                for (String activityId : uniqueActivityIds) {
                    userIds.add(activityId);
                }
            }
        }else if (classesDayOrNight.equals("night")){
            Station stationTime  =  stationMapper.selectStationById(stationId.intValue());
            if (stationTime.getNightShiftUserIds() != null && stationTime.getNightShiftUserIds() != "") {
                String[] uniqueActivityIds = stationTime.getNightShiftUserIds().split(",");
                for (String activityId : uniqueActivityIds) {
                    userIds.add(activityId);
                }
            }
        }
        if(userIds.size()>0){

        }else{
            userIds = null;
        }


        String roleId = dictDataMapper.selectDictValue(DIC_TYPE_STAFF_SETTING, DIC_LABEL_STAFF_ROLE_ID);
        List<StaffVO> staffs = staffMapper.queryStaffList(stationId,Integer.parseInt(roleId),userIds);
        List<StaOilGun> oilGuns = queryStationGunList(stationId);
        Iterator<StaffVO> iterator = staffs.iterator();
        while (iterator.hasNext()) {
            StaffVO staff = iterator.next();
            String gunNames = staff.getGun();
            if (StringUtils.isBlank(gunNames)) {
                staff.setGunList(Collections.emptyList());
                continue;
            }
            String[] split = gunNames.split(",");
            ArrayList<StaOilGun> gunList = new ArrayList<>();
            for (StaOilGun oilGun : oilGuns) {
                if (StringUtils.equalsAny(oilGun.getGunName(), split)) {
                    gunList.add(oilGun);
                }
            }
            staff.setGunList(gunList);
        }
        return staffs;
    }
}
